//
//  OtherDeviceViewController.m
//  EZ Tracker
//
//  Created by FENGYL on 2017/1/5.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

#import "OtherDeviceViewController.h"
#import "UserModel.h"
#import "NotificationsViewController.h"

@interface OtherDeviceViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *otherDeviceTableView;
@property (strong, nonatomic) UserModel * userModel;

@end

@implementation OtherDeviceViewController

-(UserModel *)userModel{
    
    if (!_userModel) {
        
        _userModel = [[UserModel alloc]init];
        
    }
    
    return _userModel;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.otherDeviceTableView.delegate = self;
    self.otherDeviceTableView.dataSource = self;
    
    self.otherDeviceTableView.tableFooterView = [[UIView alloc]init];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark TableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.userModel.deviceByAccountArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"OtherDeviceCell"];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OtherDeviceCell"];
        
    }
    
    NSDictionary * dict = self.userModel.deviceByAccountArray[indexPath.row];
    
    //设置deviceName
    
    NSString *deviceName;
    
    if ([self.userModel.changeDeviceNameDictionary valueForKey:[dict valueForKey:NC_SERIAL_NUMBER]]) {
        
        deviceName = [self.userModel.changeDeviceNameDictionary valueForKey:[dict valueForKey:NC_SERIAL_NUMBER]];
        
    }else{
        
        deviceName = [dict valueForKey:NC_DISPLAY_NAME];
        
    }
    
//    NSString * string = [dict valueForKey:@"DisplayName"];
    
    [cell.textLabel setText:deviceName];
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NotificationsViewController *notificationsViewController = [self.navigationController.viewControllers objectAtIndex:(self.navigationController.viewControllers.count) - 2];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    notificationsViewController.selectSerialNumber = cell.textLabel.text;
    
    [notificationsViewController beginTableViewMjHeaderReload];
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
//    notificationsViewController.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
